<template>
  <div>
    <section class="login isPc">
      <div class="info">
        <div class="from">
          <p class="title">会员登陆</p>
          <p><span class="stitle">电子邮箱地址: </span>
            <input type="text" name="" v-model="name" placeholder="berenice.gusikowski@gmail.com">
          </p>
          <p><span class="stitle">密码: </span>
            <input type="password" @keyup.enter="clickLogin" name="" v-model="password" placeholder="xxxxxxx"><span class="msg"></span></p>
          <button @click="clickLogin">登录</button>
        </div>
        <div class="register">
          <p class="title">尚未注册会员？</p>
          <p>初次使用MiMi Market官方网络商城的客人请进行会员注册。</p>
          <p>注册成为会员后您在购物时无须再次填写个人信息。</p>
          <button @click="register">立即注册</button>
        </div>
      </div>
      <div class="huiBg" @click="closeLogin"></div>
    </section>
  </div>
  
</template>
<script>
import axios from 'axios'
import {Login} from '@/config/api.js'
export default {
  name: 'hello',
  data() {
    return {
      name: '',
      password: ''
    }
  },
  methods: {
    register() {
      //发送注册请求
      this.$store.dispatch("closeloginBox");
      this.$router.push('/register')
    },
    async clickLogin() {

      if (this.name != '' && this.password != '') {

        let name = this.name
        let password = this.password

        let date = {
            "grant_type": "password",
            "client_id": "app",
            //"client_secret": "WdmxLMvCngT8O5zKUpXICH0ZOdCjot5qTl1rdk4T",
            "client_secret": "Mi2y$10$2EWnVYGTqunVafKUcT4oPrfYNxIQZRFn",
            "scope": "app",
            "username": name,
            "password": password
        }
        this.$store.dispatch("isLoading",true)
        await Login(date).then(function(reg){
            let token = reg.data.access_token
            var arr = token.split('');
            var passToken = "";
            for (var i = 0; i < token.length; i++) {
              passToken += arr[i].charCodeAt();
              passToken += "/";
            }
            var lastToken = passToken.substring(0, passToken.length - 1)
            var d = new Date();
            d.setTime(d.getTime() + (604800000));
            var expires = "expires=" + d.toUTCString();
            document.cookie = "login" + "=" + lastToken + "; " + expires;
            this.$store.dispatch("loginsync",token);
            window.location.reload();
            console.log('登录成功')
        }.bind(this)).catch(function(err){
          alert(err.data.message)
        })
        this.$store.dispatch("isLoading",false)
        
        
      } else {
        alert('请输入账号密码！')
      }

    },
    //关闭登录窗
    closeLogin(){
      this.$store.dispatch("closeloginBox");
    }

  }
}

</script>
